package com.hmeter.mongodb;

import java.net.UnknownHostException;
import java.util.List;

import com.mongodb.BasicDBObject;
import com.mongodb.WriteResult;

public class MongoDbUpdateSamplerUtility {

	private MongoDbUtility mongoDbUtility = null;
	
	public MongoDbUpdateSamplerUtility(String host, int port, String dbName, String collectionName) throws UnknownHostException
	{
		mongoDbUtility = new MongoDbUtility(host, port, dbName, collectionName);
	}
	
	public MongoDbUtility getMongoDbUtility()
	{
		return mongoDbUtility;
	}
	
	public String updateExistingDocumentsInCollection(List<BasicDBObject> queries, String operator, BasicDBObject updateValue)
	{
		BasicDBObject searchQuery = new BasicDBObject();
		searchQuery.put(operator, queries);	
		BasicDBObject doc = new BasicDBObject("$set", updateValue);
		WriteResult result = mongoDbUtility.getMongoDbCollection().update(searchQuery, doc, false, true);
		return "Number of records updated: " + result.getN();
	}
}
